<?php
// +----------------------------------------------------------------------
// | likeadmin快速开发前后端分离管理后台（PHP版）
// +----------------------------------------------------------------------
// | 欢迎阅读学习系统程序代码，建议反馈是我们前进的动力
// | 开源版本可自由商用，可去除界面版权logo
// | gitee下载：https://gitee.com/likeshop_gitee/likeadmin
// | github下载：https://github.com/likeshop-github/likeadmin
// | 访问官网：https://www.likeadmin.cn
// | likeadmin团队 版权所有 拥有最终解释权
// +----------------------------------------------------------------------
// | author: likeadminTeam
// +----------------------------------------------------------------------


namespace app\adminapi\controller\room;


use app\adminapi\controller\BaseAdminController;
use app\adminapi\lists\room\RoomApartmentDeviceLists;
use app\adminapi\logic\room\RoomApartmentDeviceLogic;
use app\adminapi\validate\room\RoomApartmentDeviceValidate;
use app\common\model\room\RoomApartmentDevice;
use app\common\service\ConfigService;


/**
 * 公寓设备关联控制器
 * Class RoomApartmentDeviceController
 * @package app\adminapi\controller\room
 */
class RoomApartmentDeviceController extends BaseAdminController {


    /**
     * @notes 获取公寓设备关联列表
     * @return \think\response\Json
     * @author likeadmin
     * @date 2024/02/26 16:32
     */
    public function lists() {
        return $this->dataLists(new RoomApartmentDeviceLists());
    }


    /**
     * @notes 添加公寓设备关联
     * @return \think\response\Json
     * @author likeadmin
     * @date 2024/02/26 16:32
     */
    public function add() {
        $params = (new RoomApartmentDeviceValidate())->post()->goCheck('add');
        $result = RoomApartmentDeviceLogic::add($params);
        if (true === $result) {
            return $this->success('添加成功', [], 1, 1);
        }
        return $this->fail(RoomApartmentDeviceLogic::getError());
    }


    /**
     * @notes 编辑公寓设备关联
     * @return \think\response\Json
     * @author likeadmin
     * @date 2024/02/26 16:32
     */
    public function edit() {
        $params = (new RoomApartmentDeviceValidate())->post()->goCheck('edit');
        $result = RoomApartmentDeviceLogic::edit($params);
        if (true === $result) {
            return $this->success('编辑成功', [], 1, 1);
        }
        return $this->fail(RoomApartmentDeviceLogic::getError());
    }

    public function setSave() {
        $params = $this->request->param();
        $res = RoomApartmentDeviceLogic::setSave($params);
        if (!$res) {
            return $this->fail(RoomApartmentDeviceLogic::getError());
        }
        return $this->success('编辑成功', [], 1, 1);
    }

    /**
     * @notes 删除公寓设备关联
     * @return \think\response\Json
     * @author likeadmin
     * @date 2024/02/26 16:32
     */
    public function delete() {
        $params = (new RoomApartmentDeviceValidate())->post()->goCheck('delete');
        RoomApartmentDeviceLogic::delete($params);
        return $this->success('删除成功', [], 1, 1);
    }


    /**
     * @notes 获取公寓设备关联详情
     * @return \think\response\Json
     * @author likeadmin
     * @date 2024/02/26 16:32
     */
    public function detail() {
        $params = (new RoomApartmentDeviceValidate())->goCheck('detail');
        $result = RoomApartmentDeviceLogic::detail($params);
        return $this->data($result);
    }

    public function copyAdd() {
        $params = input();
        $time = date("Y-m-d H:i:s");

        if ($params['device_type'] == 1) {
            $dc = ConfigService::get('room_apartment', 's_device_coefficient');
            $dp = ConfigService::get('room_apartment', 's_device_price');
        } else {
            $dc = ConfigService::get('room_apartment', 'd_device_coefficient');
            $dp = ConfigService::get('room_apartment', 'd_device_price');
            $params['device_type'] = $params['device_type'] ? $params['device_type'] : 2;
        }
        $room_model = new RoomApartmentDevice();
        $lists = $room_model->field([
            "cid", "bid", "rid", "sort",
            "'" . $params['device_type'] . "' as device_type",
            "'" . $dc . "' as coefficient",
            "'" . $dp . "' as price",
            "'0' as room_master", "'" . $time . "' as create_time", "'" . $time . "' as update_time",
        ])->whereIn("id", $params['id'])->append([
            "copy_add"
        ])->select()->toArray();
        if (!$lists) {
            return $this->fail('查询失败');
        }
        $res = $room_model->strict(false)->insertAll($lists);
        if (!$res) {
            return $this->fail('操作失败');
        }
        return $this->success('新增成功');
    }


}